package libra.sentinelRestTemplate;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;

import libra.sentinelRestTemplate.call.BlockExHandler_RestTemplate;

@RestController
@RequestMapping("dispatcher")
public class DispatcherController {
	
	@Value("${server.servlet.context-path}")
	public String servletPath;
	@Value("${server.port}")
	public String servletPort;
	/*********************** 测试RestTemplate本地服务调用 **************************/
	public String localAddr = "127.0.0.1";
	@Autowired
	public RestTemplate localSentinelRestTemplate;

	// 测试服务调用请求
	@RequestMapping("local/exec")
	public Object local_exec() {
		return localSentinelRestTemplate.postForEntity("http://"+localAddr+":"+servletPort+servletPath+"/local/exec", null, Map.class);
	};
	
	// 测试请求超时
	@RequestMapping("local/timeout")
	public Object local_timeout(){
		return localSentinelRestTemplate.postForEntity("http://"+localAddr+":"+servletPort+servletPath+"/local/timeout", null, Map.class);
	};
	
	// 测试请求处理异常
	@RequestMapping("local/ex")
	public Object local_ex( HttpServletResponse resp ){
		ResponseEntity<Map> postForEntity = localSentinelRestTemplate.postForEntity("http://"+localAddr+":"+servletPort+servletPath+"/local/ex", null, Map.class);
		return postForEntity;
	};
	
	
	/*********************** 测试RestTemplate微服务调用 **************************/
	@Autowired
	public RestTemplate serviceSentinelRestTemplate;
	// 获取配置中当前应用的服务名
	@Value("${spring.application.name}")
	public String serviceName;
	
	@RequestMapping("service/exec")
	public Object service_exec() {
		return serviceSentinelRestTemplate.postForEntity("http://" + serviceName + "/" + servletPath + "/service/exec", null, Map.class);
	};
	
	// 测试请求超时
	@RequestMapping("service/timeout")
	public Object service_timeout(){
		return serviceSentinelRestTemplate.postForEntity("http://" + serviceName + "/" + servletPath + "/service/timeout", null, Map.class);
	};
	
	// 测试请求处理异常
	@RequestMapping("service/ex")
	public Object service_ex(){
		return serviceSentinelRestTemplate.postForEntity("http://" + serviceName + "/" + servletPath + "/service/ex", null, Map.class);
	};
}
